package com.freak.neteasecloudmusic.modules.video;

import android.os.Bundle;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.freak.neteasecloudmusic.R;
import com.freak.neteasecloudmusic.base.BaseAbstractMvpFragment;
import com.freak.neteasecloudmusic.modules.base.adapter.MainTabAdapter;
import com.freak.neteasecloudmusic.modules.video.entity.VideoTagEntity;
import com.freak.neteasecloudmusic.modules.video.fragment.VideoTabFragment;
import com.freak.neteasecloudmusic.view.custom.viewpager.CustomViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.jzvd.Jzvd;


/**
 * @author freak
 * @date 2019/2/19
 */

public class VideoFragment extends BaseAbstractMvpFragment<VideoPresenter> implements VideoContract.View, ViewPager.OnPageChangeListener {
    @BindView(R.id.slidingTabLayoutVideo)
    SlidingTabLayout slidingTabLayoutVideo;
    @BindView(R.id.customViewPagerVideo)
    CustomViewPager customViewPagerVideo;
    private MainTabAdapter mainTabAdapter;
    private List<String> titleList;
    private List<Fragment> fragmentList;

    @Override
    public void showToast(String toast) {

    }

    @Override
    protected VideoPresenter createPresenter() {
        return new VideoPresenter();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_video;
    }

    @Override
    protected void initEventAndData() {
        mPresenter.getVideoTag();
    }

    @Override
    protected void initView(View view) {
        titleList = new ArrayList<>();
        fragmentList = new ArrayList<>();

    }


    @Override
    protected void initLazyData() {

    }

    @Override
    protected void showLoading() {

    }

    @Override
    public void getVideoTagSuccess(VideoTagEntity videoTagEntity) {
        titleList.clear();
        fragmentList.clear();
        titleList.add("推荐");
        VideoTabFragment videoTabFragment = new VideoTabFragment();
        Bundle bundle = new Bundle();
        bundle.putString("tab", "推荐");
        bundle.putInt("id", 0);
        videoTabFragment.setArguments(bundle);
        fragmentList.add(videoTabFragment);
        titleList.add("Look直播");
        VideoTabFragment lookVideoTabFragment = new VideoTabFragment();
        Bundle lookBundle = new Bundle();
        lookBundle.putString("tab", "Look直播");
        lookBundle.putInt("id", 1);
        lookVideoTabFragment.setArguments(lookBundle);
        fragmentList.add(lookVideoTabFragment);
        if (videoTagEntity != null) {
            if (videoTagEntity.getData() != null && videoTagEntity.getData().size() > 0) {
                boolean isFinish = false;
                for (int i = 0; i < videoTagEntity.getData().size(); i++) {
                    if (videoTagEntity.getData().get(i).getName().equals("最佳饭制")) {
                        titleList.add(videoTagEntity.getData().get(i).getName());
                        VideoTabFragment videoTabFragment1 = new VideoTabFragment();
                        Bundle bundle1 = new Bundle();
                        bundle1.putString("tab", videoTagEntity.getData().get(i).getName());
                        bundle1.putInt("id", videoTagEntity.getData().get(i).getId());
                        videoTabFragment1.setArguments(bundle1);
                        fragmentList.add(videoTabFragment1);
                        isFinish = true;
                    }
                    if (!isFinish) {
                        titleList.add(videoTagEntity.getData().get(i).getName());
                        VideoTabFragment videoTabFragment1 = new VideoTabFragment();
                        Bundle bundle1 = new Bundle();
                        bundle1.putString("tab", videoTagEntity.getData().get(i).getName());
                        bundle1.putInt("id", videoTagEntity.getData().get(i).getId());
                        videoTabFragment1.setArguments(bundle1);
                        fragmentList.add(videoTabFragment1);
                    }

                }
            }
        }
        mainTabAdapter = new MainTabAdapter(getChildFragmentManager(), fragmentList, titleList);
        customViewPagerVideo.setAdapter(mainTabAdapter);
        customViewPagerVideo.setScanScroll(true);
        customViewPagerVideo.addOnPageChangeListener(this);
        slidingTabLayoutVideo.setViewPager(customViewPagerVideo, titleList.toArray(new String[0]), mActivity, (ArrayList<Fragment>) fragmentList);
        customViewPagerVideo.setCurrentItem(0);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        //暂停所有的视频播放
        Jzvd.releaseAllVideos();
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }
}
